package KK.Controls.Window
{ 
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class Box extends Bitmap 
	{ 
		static public const MAX_GRID:int=9;//九宫格
		
		
		private var BitmapList:Vector.<BitmapData>; //块列表
//		private var LumpPositionList:Vector.<Point>;
		public function Box(bitmapData:BitmapData=null, pixelSnapping:String="auto", smoothing:Boolean=false)
		{
			//TODO: implement function
			super(bitmapData, pixelSnapping, smoothing);
		}
		
		
		
		/**
		 * 设置大小 
		 * @param width
		 * @param height
		 * 
		 */
		public function SetSize(width:int,height:int):void{
			bitmapData=new BitmapData(width,height,true,0);
			ReDraw();
		}
		/**
		 * 设置图片并截取图片 
		 * @param bitmapData
		 * @param lumpList
		 * @param 
		 * @param Point
		 * 
		 */
		public function SetBitmap(bitmapData:BitmapData,lumpList:Vector.<Rectangle>):void{
			var BitmapList:Vector.<BitmapData>=new Vector.<BitmapData>(MAX_GRID,true);
			if(lumpList.length<MAX_GRID){
				throw new Error("Box:块坐标点长度不符");
			}
			var point:Point=new Point(0,0);
			var i:int=0;
			while(i<MAX_GRID){
				BitmapList[i]=CutPicture(bitmapData,lumpList[i],point);
				i++;
			}
			
			SetLumpList(BitmapList);
		}
		/**
		 * 设置各块数据 
		 * @param BitmapList
		 * 
		 */
		public function SetLumpList(BitmapList:Vector.<BitmapData>):void{
			this.BitmapList=BitmapList;
		}
		
		/**
		 * 重绘 
		 * 
		 */
		protected function ReDraw():void{
			if(width<=0&&height<=0){
				//未能绘制大小
				return ;
			}
			
			Draw();
		}
		
		
		
		/**
		 * 绘制图片 
		 * 
		 */
		private function Draw():void{ 
			
			var point:Point=new Point();
			var destPoint:Point=new Point();
			var rect:Rectangle=new Rectangle();
			var rectList:Vector.<Rectangle>=ComputsRect();
			var i:int=0;
			const len:int=rectList.length;
			
			while(i<len){
				point.y=point.x=0;
				while(point.y<rectList[i].height){
					while(point.x<rectList[i].width){
						rect=rectList[i].clone();
						rect.width=BitmapList[i].width>rectList[i].width-point.x?rectList[i].width-point.x:BitmapList[i].width;
						rect.height=rectList[i].height-point.y>BitmapList[i].height?rectList[i].height-point.x:BitmapList[i].height;
						destPoint.x=rect.x+point.x;
						destPoint.y=rect.y+point.y;
						rect.y=rect.x=0;
						bitmapData.copyPixels(BitmapList[i],rect,destPoint);
						
						point.x+=rect.width;
					}
					point.x=0;
					point.y+=rect.height;
				}
				
				i++;
			}
			
			
			
		}
		
		
		/**
		 * 计算宽度数据 
		 * @return 
		 * 
		 */
		private function ComputsRect():Vector.<Rectangle>{
			var result:Vector.<Rectangle>=new Vector.<Rectangle>(MAX_GRID,true);
			var i:int,j:int,len:int=3;
			var point:Point=new Point();
			var tmp:Rectangle; 
			for(i=0;i<len;i++){
				for(j=0;j<len;j++){
					tmp=new Rectangle();
					result[i*len+j]=tmp;
					
					
					tmp.x=point.x;
					tmp.y=point.y; 
					//计算高宽
					switch(true){
						case i%2==1&&j%2==1:
							//中心
							tmp.width=width-(BitmapList[0].width+BitmapList[2].width);
							tmp.height=height-(BitmapList[0].height+BitmapList[6].height);
							break;
						case j%2==1:
							//横向中心 
							tmp.width=width-(BitmapList[0].width+BitmapList[2].width);
							tmp.height=BitmapList[i*len+j].height; 
							break;
						case i%2==1:
							//竖向中心
							tmp.width=BitmapList[i*len+j].width;
							tmp.height=height-(BitmapList[0].height+BitmapList[6].height);
							break;
						default:
							tmp.width=BitmapList[i*len+j].width;
							tmp.height=BitmapList[i*len+j].height; 
							break;
					}
					
					point.x+=tmp.width;
				}
				point.y+=result[i*len].height;
				point.x=0;
			} 
			return result;
		}
		
		
		
		/**
		 * 裁剪图片 
		 * @param source 源图片
		 * @param rect 裁剪大小
		 * @param destPoint 粘贴位置
		 * @return 裁剪后的图片
		 * 
		 */
		private function CutPicture(source:BitmapData,rect:Rectangle,destPoint:Point):BitmapData{
			var result:BitmapData;
			if(rect.width>0&&rect.height>0){
				result=new BitmapData(rect.width,rect.height,true,0);
				result.copyPixels(source,rect,destPoint);
			}
			return result;
		} 
		
	}
}